#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e4 + 5;

int n, m;
int a[N];
vector<int> ch[N];
int dp[N];
int ans = -2e9;
void dfs(int u, int fu) {
  dp[u] = a[u];
  for (int v : ch[u]) {
    if (v == fu) continue;
    dfs(v, u);
    dp[u] += max(0, dp[v]);
  }
  ans = max(ans, dp[u]);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 2, n) {
    int u, v;
    cin >> u >> v;
    ch[u].push_back(v);
    ch[v].push_back(u);
  }
  dfs(1, 1);
  cout << ans;
  return 0;
}